package org.dromara.job.dnnyun.processors;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dromara.curd.dnnyun.domain.bo.DnnyunStoreAchievementBo;
import org.dromara.data.dnnyun.service.IDnnyunDataService;
import org.springframework.stereotype.Component;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;
import tech.powerjob.worker.log.OmsLogger;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 同步门店业绩列表
 *
 * @author wdm
 * @since 2024/3/18
 */
@Slf4j
@Component
public class StoreAchievementListProcessor implements BasicProcessor {
    @Resource
    private IDnnyunDataService dnnyunDataService;

    @Override
    /**
     * 参数格式{"beginOrderDate":"2024-03-18 00:00:00","endOrderDate":"2024-03-18 23:59:59"}
     */
    public ProcessResult process(TaskContext context) throws Exception {
        OmsLogger omsLogger = context.getOmsLogger();
        omsLogger.info("dnnyun storeAchievementListProcessor,context is {}.", context);

        log.info("jobParam:{}", context.getJobParams());
        log.info("instanceParams:{}", context.getInstanceParams());
        String param  = StringUtils.isBlank(context.getInstanceParams()) ? context.getJobParams() : context.getInstanceParams();

        //默认同步当天的记录
        Map<String,Object> paramMap = new HashMap<>();
        Date now = DateUtil.date();
        paramMap.put("beginOrderDate", DateUtil.format(DateUtil.beginOfDay(now), DatePattern.NORM_DATETIME_PATTERN));
        paramMap.put("endOrderDate", DateUtil.format(DateUtil.endOfDay(now), DatePattern.NORM_DATETIME_PATTERN));
        DnnyunStoreAchievementBo bo = new DnnyunStoreAchievementBo();
        if(StringUtils.isNotBlank(param)){
            paramMap = JSONUtil.toBean(param, HashMap.class);
        }
        bo.setParams(paramMap);
        dnnyunDataService.syncStoreAchievementList(bo);
        boolean success = true;
        omsLogger.info("dnnyun storeAchievementListProcessor finished process,success: {}", success);
        return new ProcessResult(success, context + ": " + success);
    }
}
